package com.yxw.blog.controller;

import com.yxw.blog.annotation.UnInterception;
import com.yxw.blog.bean.entity.User;
import com.yxw.blog.service.RegisterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;

/**
 * @author yexinwei
 */
@Api(value = "用户注册控制层")
@Controller
public class RegisterController {
    @Autowired
    RegisterService registerService;

    @UnInterception
    @GetMapping("/Register")
    public String toRegister(){
        return "register";
    }

    @UnInterception
    @PostMapping("/checkName")
    @ResponseBody
    public String checkName(@RequestParam("name") String name ){
        User user = registerService.checkName(name);
        if(user!=null){
            return "用户已存在";
        }else{
            return "";
        }
    }

    @ApiOperation(value = "用户注册")
    @UnInterception
    @PostMapping("/Register")
    public String register(@RequestParam("name") @ApiParam(value = "用户名") String name , @RequestParam("password") @ApiParam(value = "密码") String password , Model model){
        //String defaultHead = "D:\\blog\\head.jpg";
        String defaultHead = System.getProperty("user.dir")+"\\head.jpg";
        System.out.println(defaultHead);
        Integer res = registerService.register(name,password,defaultHead);
        if(res==1){
            //通过用户名获得User
            User user = registerService.getNewUser(name);
            //为每一个新用户创建一个文件夹存放数据
            File userFile = new File(System.getProperty("user.dir")+"\\User\\"+user.getUserName());
            System.out.println(userFile.getPath());
            if(!userFile.exists()){
                userFile.mkdirs();
            }
            //在每个用户的文件夹下面创建一个存放文章的文件夹Article
            File articleFile = new File(System.getProperty("user.dir")+"\\User\\"+user.getUserName()+"\\Article");
            if(!articleFile.exists()){
                articleFile.mkdirs();
            }
            //在每个用户的文件夹下面创建一个存放头像的文件夹Head
            File headFile = new File(System.getProperty("user.dir")+"\\User\\"+user.getUserName()+"\\Head");
            if(!articleFile.exists()){
                headFile.mkdirs();
            }
            //在每个用户的文件夹下面创建一个存放文章图片的文件夹ArticleImage
            File ArticleImageFile = new File(System.getProperty("user.dir")+"\\User\\"+user.getUserName()+"\\ArticleImage");
            if(!articleFile.exists()){
                ArticleImageFile.mkdirs();
            }
            return "index";
        }else{
            model.addAttribute("info","注册失败");
            return "register";
        }
    }

}
